package com.uff.hmstpa.model.persistence.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;

import com.uff.hmstpa.model.domain.Specialty;
import com.uff.hmstpa.model.persistence.AbstractModelDAO;
import com.uff.hmstpa.model.persistence.SpecialtyDAO;

public class SpecialtyDAOImpl extends AbstractModelDAO implements SpecialtyDAO {

	private static SpecialtyDAOImpl SOLE_INSTANCE;

	private SpecialtyDAOImpl() {
		super();
	}

	public static SpecialtyDAO getInstance() {
		if (SOLE_INSTANCE == null) {
			SOLE_INSTANCE = new SpecialtyDAOImpl();
		}
		return SOLE_INSTANCE;
	}

	public Specialty retrieve(int code) {
		String sql = "SELECT * FROM specialty WHERE code = ?";
		Object[] params = new Object[]{code};
		return jdbcTemplate.queryForObject(sql, params, new SpecialtyMapper());
	}

	public List<Specialty> load() {
		String sql = "SELECT * FROM specialty";
		return jdbcTemplate.query(sql, new SpecialtyMapper());
	}

	private static final class SpecialtyMapper implements RowMapper<Specialty> {

		public Specialty mapRow(ResultSet rs, int rowNum) throws SQLException {
			Specialty specialty = new Specialty();
			specialty.setCode(rs.getInt("code"));
			specialty.setName(rs.getString("name"));
			specialty.setAppointmentPrice(rs.getDouble("appointmentPrice"));
			return specialty;
		}
	}
}
